"""
Case Type   : tools
Case Name   : 执行初始化数据库命令gs_initdb：系统默认区域的编码格式和用--encoding参数
                指定的编码格式匹配
Create At   : 2021/12/13
Owner       : opentestcase012
Description :
    1.执行命令：gs_initdb -D $data/ --nodename=single  --locale=yo_NG.utf8
    -encoding=UTF8
Expect      :
    1.初始化成功，在data目录下生成初始化后的文件
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger


class Tools(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info('----Opengauss_Function_gs_initdb_Case0010 start----')
        self.userNode = Node('PrimaryDbUser')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.dir_path = os.path.join(macro.DB_INSTANCE_PATH, 'datadir')

    def test_systools(self):
        self.logger.info('--------执行gs_initdb命令--------')
        excute_cmd1 = f'source {self.DB_ENV_PATH};gs_initdb -D ' \
                      f'{self.dir_path} --nodename=single -w ' \
                      f'{self.userNode.ssh_password} --locale=zh_CN.utf8 ' \
                      f'--encoding=UTF8'
        self.logger.info(excute_cmd1)
        msg1 = self.userNode.sh(excute_cmd1).result()
        self.logger.info(msg1)
        self.assertTrue(msg1.find('Success. You can now start the database '
                                  'server of single node') > -1)
        self.logger.info('--------生成datadir目录--------')
        excute_cmd2 = f'ls {self.dir_path}'
        self.logger.info(excute_cmd2)
        msg2 = self.userNode.sh(excute_cmd2).result()
        self.logger.info(msg2)
        self.assertTrue('postgresql.conf' in msg2 and 'pg_hba.conf' in msg2)
        self.logger.info('--------查看到编码格式--------')
        excute_cmd3 = f'locale|grep LC_CTYP'
        self.logger.info(excute_cmd3)
        msg3 = self.userNode.sh(excute_cmd3).result()
        self.logger.info(msg3)
        self.assertTrue(msg3.find('en_US.UTF-8') > -1)

    def tearDown(self):
        self.logger.info('-----------清理环境-----------')
        excute_cmd1 = f'rm -rf {self.dir_path};ls {self.dir_path}'
        self.logger.info(excute_cmd1)
        msg1 = self.userNode.sh(excute_cmd1).result()
        self.logger.info(msg1)
        self.assertTrue(msg1.find('No such file or directory') > -1)
        self.logger.info('---Opengauss_Function_gs_initdb_Case0010 finish---')
